/**
String扩展
*/

import 'package:wgq_dart/src/comm_function/core.dart';

/*************************************************
    字符串扩展
 ************************************************/

/// 强制字符串长度
/// 如果长度超过就截取,如果不足就在前面补满指定的字符
extension WDStringExt on String {
  String forceLength(int length, {String? completeStr}) {
    completeStr ??= "3";
    if (this.length == length) {
      return this;
    }
    if (this.length > length) {
      return this.substring(0, length);
    } else {
      final needTime = length - this.length;
      String newString = this;
      for (int i = 0; i < needTime; i++) {
        newString += completeStr;
      }
      return newString;
    }
  }

  /// 把字符串核心的字符用*号隐藏
  String hideCoreStr(
      {
      //要显示的遮盖符号的总数
      int starSymbolCount = 17,
      //指定遮盖符号
      final String symbol = "*",
      //头尾要保留的字符数
      int keep = 3}) {


    String symbolStr = symbol;

    for (int i = 0; i < starSymbolCount - 1; i++) {
      symbolStr += symbol;
    }

    if (WDDataType.isAir(this)) return this;
    if (this.length <= keep) {
      return '${this}$symbolStr';
    }

    // if (this.length < 5) {
    //   return '${this.substring(0, 1)}$symbolStr${this.substring(this.length - 1)}';
    // }

    return '${this.substring(0, keep)}$symbolStr${this.substring(this.length - keep)}';
  }

  /// 判断是否是邮箱
  bool isAEmail() {
    /// 邮箱正则
    final String regexEmail =
        "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$";

    /// 检查是否是邮箱格式
    if (this == null || this.isEmpty) return false;
    return RegExp(regexEmail).hasMatch(this);
  }
}

/*************************************************
    int扩展
 ************************************************/

/*************************************************
    double扩展
 ************************************************/
